package ar.edu.utn.frgp.mvc.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.edu.utn.frgp.banco.domain.Cuenta;
import ar.edu.utn.frgp.beans.MessageBean;
import ar.edu.utn.frgp.banco.business.Implements.ImpCajaAhorroBusiness;
import ar.edu.utn.frgp.banco.business.Implements.ImpCuentaCorrienteBusiness;
/**
 * Servlet implementation class CuentasServlet
 */
public class CuentasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CuentasServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		MessageBean estado = new MessageBean();
		if (request.getParameter("accion").equals("Agregar")) {
			estado.setMensaje("Cargado");
			request.setAttribute("estado", estado);
			request.getRequestDispatcher("Cuentas.jsp").forward(request,
					response);
		} else if (request.getParameter("accion").equals("Crear Cuenta")) {
			int flagOK = 0;
			try{
				Integer id_cliente = Integer.parseInt(request.getSession().getAttribute("ID_Cliente").toString());
				Cuenta cuenta = new Cuenta();
				
				if (cuenta.TIPOCUENTA_CAJAAHORRO.equals(request.getParameter("tipo"))){
					ImpCajaAhorroBusiness cajaahorrobusiness = new ImpCajaAhorroBusiness();
					cajaahorrobusiness.CrearCajaAhorro(id_cliente);
				}else{
					ImpCuentaCorrienteBusiness cuentacorrientebusiness = new ImpCuentaCorrienteBusiness();
					cuentacorrientebusiness.CrearCuentaCorriente(id_cliente);
				}
			}catch (Exception e){
				MessageBean aux = new MessageBean();
				aux.setMensaje(e.getMessage());
				request.setAttribute("msj", aux);
				flagOK = 1;
			}
			if (flagOK == 0){
				request.getRequestDispatcher("../Blanco.jsp").forward(request,
						response);
				
			}else{
				request.getRequestDispatcher("Cuentas.jsp").forward(request,
						response);
			}
		}
	}

}
